import logging
import requests
from conf.Readini import readIni


class orderRequest:
    def __init__(self):
        # 配置日志
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.INFO)  # 设置日志级别
        handler = logging.StreamHandler()  # 输出到控制台
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

    def send_request(self, method, uri, header, **kwargs):
        try:
            ini = readIni()
            self.logger.info(f"请求接口：{uri}，方法：{method}，头信息：{header}")  # 请求信息日志
            r = requests.request(method, f"{ini.get_ip_port()}{uri}", headers=header, **kwargs)
            if r.status_code == 200:
                self.logger.info(f"接口请求成功，返回数据：{r.json()}")  # 请求成功日志
            else:
                self.logger.error(f"接口请求失败，状态码：{r.status_code}")  # 请求失败日志
            return r
        except Exception as e:
            self.logger.error(f"发生异常接口请求失败，{e}")  # 异常日志
            return None
